package cn.kp.learning.controller;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.kp.learning.model.CoreUserOrg;
import cn.kp.learning.model.CoreUserOrgExample;
import cn.kp.learning.model.CoreUserOrgRole;
import cn.kp.learning.model.CoreUserOrgRoleExample;
import cn.kp.learning.model.Roles;
import cn.kp.learning.model.CoreUserOrgExample.Criteria;
import cn.kp.learning.service.CoreUserOrgRoleService;
import cn.kp.learning.util.ResponseUtil;

@Controller
@RequestMapping("/userOrgRoleCon")
public class UserOrganizationRoleController {

	@Resource
	private CoreUserOrgRoleService coreUserOrgRoleService;
	
	//user-org-roles
	@RequestMapping("/addRoles")
	public void addRoles(HttpServletResponse response,HttpServletRequest request, CoreUserOrgRole cuor) throws Exception{
		JSONObject json=new JSONObject();
		System.out.println("user-org add :" + cuor.getOrgCompanyId());
		int orgaddresult = coreUserOrgRoleService.addCuor(cuor);
		System.out.println("user-org Action : "+orgaddresult);
		if(orgaddresult>0){
			json.put("success", true);
		}else{
			json.put("success", false);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//user-org-roles delete
	@RequestMapping("/deleteRoles")
	public void deleteRoles(HttpServletResponse response,HttpServletRequest request, Roles rl) throws Exception{
		
	}
	//user-org-roles update
	@RequestMapping("/updateRoles")
	public void updateRoles(HttpServletResponse response,HttpServletRequest request, Roles rl) throws Exception{
		
	}
	
	//user-org-roles select
	@RequestMapping("/getRoles")
	public void getRoles(HttpServletResponse response,HttpServletRequest request, Roles rl) throws Exception{
		System.out.println("get user-org-role list ... ");
		String personUserName = request.getParameter("userName");
		System.out.println("get para : " + request.getParameter("userName"));
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));
		
		List<CoreUserOrgRole> lt = null;
		CoreUserOrgRoleExample example = new CoreUserOrgRoleExample();
		cn.kp.learning.model.CoreUserOrgRoleExample.Criteria cc = example.createCriteria();
		cc.andUserNameEqualTo(personUserName);
		String startRows = request.getParameter("start");
		String pagesLimit = request.getParameter("limit");
		if(null!=startRows && !startRows.equals("")){
			example.setStartRow(Integer.parseInt(startRows));
		}
		if(null!=pagesLimit && !pagesLimit.equals("")){
			example.setPageSize(Integer.parseInt(startRows) + Integer.parseInt(pagesLimit));
		}
		String search1 = request.getParameter("search1");
		String search2 = request.getParameter("search2");
		if(null!=search1 && !search1.equals("")){
			cc.andAllNameLike("%"+request.getParameter("search1") +"%");
		}
		if(null!=search2 && !search2.equals("")){
			cc.andUserNameLike("%"+request.getParameter("search2") + "%");
		}
		
		int countPeople = coreUserOrgRoleService.countCuor(example);
		lt = coreUserOrgRoleService.getCuorList(example);
		
		JSONObject json = new JSONObject();
		//
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("pages", lt.size()/10+1);
		json.put("total", countPeople);
		json.put("rows", jsonArray);
		
		System.out.println("User-Org Json : " + json.toString());
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("get User-Org size " + lt.size());
	}
	
	
}
